using System;
using System.Collections.Generic;
using System.Text;
using InfraStructureLayer;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
using System.Data;
using DataLayer;
using DataAccessLayer;

namespace BusinessLayer
{
    public class BusinessBase
    {
        public BusinessBase()
        {

        }
        private bool _HasErrors;
        public bool HasErrors
        {
            get { return _HasErrors; }
        }
        private string _ErrorMessage = String.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
            set
            {
                _ErrorMessage = value;
                _HasErrors = (value.Length != 0);
            }
        }
        public static string GetErrorMessageMultilanguageString(int Id, object[] parameters)
        {
            return String.Format(CurrentContext.ErrorMessagesMultilanguage[CurrentContext.LanguageId.ToString() + "|" + Id.ToString()].ToString(), parameters);
        }
        public static string GenerateHandlerLink(string link)
        {
            if (link == null)
            {
                return "";
            }
            Crypto c = new Crypto();
            string e = @"<a\s{1}href=""(?<url>.*?)""(\s?target=""(?<target>_(blank|new|parent|self|top))"")?(\s?class=""(?<class>.*?)"")?(\s?style=""(?<style>.*?)"")?>(?<title>.*?)</a>";
            MatchCollection mc = Regex.Matches(link, e, RegexOptions.IgnoreCase);
            foreach (Match mm in mc)
            {
                string[] a = Regex.Split(mm.Value, e, RegexOptions.IgnoreCase);
                link = link.Replace(@"href=""" + a[1] + @"""", @"href=""" + InfraStructureLayer.CurrentContext.Parameters["WebSiteAddress"].ToString() + "Handlers/Redirect.ashx?Address=" + c.EncryptURL(a[1]) + @"""");
            }
            return link;
        }

        private string Make2Chars(string s)
        {
            if (s.Length == 1)
            {
                return "0" + s;
            }
            return s;
        }
        private void GenerateSiteMapXmlForFirstUse()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(System.Web.HttpContext.Current.Server.MapPath("sitemap.xml"));

            DataTable insDataTableSiteMap = new datSiteMap().SiteMap(new DbConnector());

            foreach (DataRow dr in insDataTableSiteMap.Rows)
            {
                XmlElement subElement = doc.CreateElement("", "url", "http://www.google.com/schemas/sitemap/0.84");

                doc.DocumentElement.AppendChild(subElement);


                XmlElement loc = doc.CreateElement("", "loc", "http://www.google.com/schemas/sitemap/0.84");
                loc.InnerText = dr["loc"].ToString();

                XmlElement lastMod = doc.CreateElement("", "lastmod", "http://www.google.com/schemas/sitemap/0.84");
                DateTime d = Convert.ToDateTime(dr["lastmod"]);
                lastMod.InnerText = d.Year.ToString() + "-" + Make2Chars(d.Month.ToString()) + "-" + Make2Chars(d.Day.ToString()) + "T" + Make2Chars(d.Hour.ToString()) + ":" + Make2Chars(d.Minute.ToString()) + ":" + Make2Chars(d.Second.ToString()) + "+00:00";


                XmlElement changefreq = doc.CreateElement("", "changefreq", "http://www.google.com/schemas/sitemap/0.84");
                changefreq.InnerText = dr["changefreq"].ToString();

                XmlElement priority = doc.CreateElement("","priority","http://www.google.com/schemas/sitemap/0.84");
                priority.InnerText = dr["priority"].ToString();

                subElement.AppendChild(loc);
                subElement.AppendChild(lastMod);
                subElement.AppendChild(changefreq);
                subElement.AppendChild(priority);


            }
            doc.Save(System.Web.HttpContext.Current.Server.MapPath("sitemap.xml"));
        }
        public void UpdateSiteMap()
        {
            
                XmlDocument doc;
                XmlNode node;
                XmlElement rootElement;
                XmlElement subElement;

                doc = new XmlDocument();

                node = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                doc.AppendChild(node);
                rootElement = doc.CreateElement("", "urlset", "http://www.google.com/schemas/sitemap/0.84");

                doc.AppendChild(rootElement);

            

                doc.Save(System.Web.HttpContext.Current.Server.MapPath("sitemap.xml"));

                GenerateSiteMapXmlForFirstUse();
          

        }
    }
}
